package com.b2b.util;

import com.b2b.model.InquiryListDto;
import com.b2b.model.entity.Inquiry;
import com.b2b.model.entity.InquiryMaterial;
import com.b2b.model.entity.RequirementOfMaterial;
import com.b2b.service.RequirementOfMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class RequirementOfMaterialUtil {
    private final RequirementOfMaterialService requirementOfMaterialService;
    private final InquiryMaterialUtil inquiryMaterialUtil;
    @Autowired
    public RequirementOfMaterialUtil(RequirementOfMaterialService requirementOfMaterialService,InquiryMaterialUtil inquiryMaterialUtil){
        this.requirementOfMaterialService = requirementOfMaterialService;
        this.inquiryMaterialUtil = inquiryMaterialUtil;
    }

    public Integer insertRequirementOfMaterial(RequirementOfMaterial requirementOfMaterial){
        int count = requirementOfMaterialService.insert(requirementOfMaterial);
        if (count<=0){
            Integer Id = -1;
            return Id;
        }else {
            Integer Id = requirementOfMaterial.getRequirementId();
            return Id;
        }
    }
//    public Integer insertRequirementOfMaterial(InquiryListDto inquiryListDto){
//        RequirementOfMaterial requirementOfMaterial = new RequirementOfMaterial();
//        requirementOfMaterial = getRequirementOfMaterial(inquiryListDto);
//        Integer count = insertRequirementOfMaterial(requirementOfMaterial);
//        return count;
//    }

    public Integer deleteInquiry(Integer requirementId){
        RequirementOfMaterial requirementOfMaterial = new RequirementOfMaterial();
        requirementOfMaterial.setRequirementId(requirementId);
        Integer count = requirementOfMaterialService.deleteByPrimaryKey(requirementOfMaterial);
        return count;
    }

    public Integer updateInquiry(RequirementOfMaterial requirementOfMaterial){
        Integer count = requirementOfMaterialService.updateByPrimaryKeySelective(requirementOfMaterial);
        return count;
    }

    public RequirementOfMaterial getByRequirementId(Integer requirementId){
        RequirementOfMaterial requirementOfMaterial = selectRequirementOfMaterial(requirementId);
        return requirementOfMaterial;

    }

    public List<RequirementOfMaterial> getAll(Integer inquiryId){
        List<InquiryMaterial> inquiryMaterialList = inquiryMaterialUtil.getInquiryMaterialByInquiryId(inquiryId);
        List<RequirementOfMaterial> requirementOfMaterialList = new ArrayList<RequirementOfMaterial>();
        for (int i = 0; i < inquiryMaterialList.size(); i++) {
            requirementOfMaterialList.add(selectRequirementOfMaterial(inquiryMaterialList.get(i).getRequirementId()));
        }
        return requirementOfMaterialList;
    }


    public RequirementOfMaterial selectRequirementOfMaterial(Integer requirementId){
        RequirementOfMaterial requirementOfMaterial = new RequirementOfMaterial();
        requirementOfMaterial.setRequirementId(requirementId);
        requirementOfMaterial = requirementOfMaterialService.selectByPrimaryKey(requirementOfMaterial);
        return requirementOfMaterial;
    }
//    public RequirementOfMaterial getRequirementOfMaterial(InquiryListDto inquiryListDto){
//        RequirementOfMaterial requirementOfMaterial = new RequirementOfMaterial();
//
//        requirementOfMaterial.setMaterialCode(inquiryListDto.getMaterialCode());
//        requirementOfMaterial.setMaterialName(inquiryListDto.getMaterialName());
//        requirementOfMaterial.setMaterialCategory(inquiryListDto.getMaterialCategory());
//        requirementOfMaterial.setMaterialBrand(inquiryListDto.getMaterialBrand());
//        requirementOfMaterial.setMaterialModel(inquiryListDto.getMaterialModel());
//        requirementOfMaterial.setMaterialDescription(inquiryListDto.getMaterialDescription());
//        requirementOfMaterial.setBuyNumber(inquiryListDto.getBuyNumber());
//        requirementOfMaterial.setMaterialUnit(inquiryListDto.getMaterialUnit());
//        requirementOfMaterial.setInternalPostscript(inquiryListDto.getInternalPostscript());
//        requirementOfMaterial.setExternalPostscript(inquiryListDto.getExternalPostscript());
//        requirementOfMaterial.setEnclosureUrl(inquiryListDto.getEnclosureUrlForM());
//
//        return requirementOfMaterial;
//    }
}
